package com.example.finance_manager.pages.newly_increased;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.finance_manager.R;
import com.example.finance_manager.dao.SalaryDao;
import com.example.finance_manager.models.Salary;

import java.util.List;

public class AddIncomeActivity extends AppCompatActivity {

    private EditText etMoney;
    private EditText etTime;
    private EditText etType;
    private EditText etPay;
    private EditText etOther;
    private Button btnSubmit;
    private Button btnCancel;
    private List<Salary> salaries;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_income);

        initView();
        initListener();
    }

    private void initView() {
        etMoney = (EditText) findViewById(R.id.et_add_income_money);
        etTime = (EditText) findViewById(R.id.et_add_income_time);
        etType = (EditText) findViewById(R.id.et_add_income_type);
        etPay = (EditText) findViewById(R.id.et_add_income_pay);
        etOther = (EditText) findViewById(R.id.et_add_income_other);

        btnSubmit = (Button) findViewById(R.id.btn_add_income_submit);
        btnCancel = (Button) findViewById(R.id.btn_add_income_cancel);
    }

    private void initListener() {
        btnSubmit.setOnClickListener(view -> { submit(); });
        btnCancel.setOnClickListener(view -> { cancel(); });
    }

    private void submit() {
        String money, time, type, pay, other;
        money = etMoney.getText().toString();
        time = etTime.getText().toString();
        type = etType.getText().toString();
        pay = etPay.getText().toString();
        other = etOther.getText().toString();
        Salary salary = new Salary(time, pay, type, other, Double.parseDouble(money));
        SalaryDao salaryDao = new SalaryDao(AddIncomeActivity.this);
        long r = salaryDao.add(salary);
        System.out.println(r);
        showToast("添加成功");
        clear();
    }

    private void clear() {
        etMoney.setText("");
        etTime.setText("");
        etType.setText("");
        etPay.setText("");
        etOther.setText("");
    }

    private void cancel() {
        System.out.println("取消");
        this.finish();
    }

    private void showToast(String text) {
        Toast.makeText(AddIncomeActivity.this, text, Toast.LENGTH_LONG).show();
    }
}